<?php
//$Id: AreadataController.php 607 2012-12-16 08:44:10Z tomqin@gmail.com $

/**
 * Part of the framework.
 *
 * @package     Controller
 * @version     1.0
 * @copyright   Copyright (C) 2010-2012 SupeFun.com Inc.
 * @author      TomQin <tomqin@gmail.com>
 * @license     http://www.supefun.com/license/
 */

Class AreadataController extends AdminController
{

    public function dlglistAjaxAction() {
        $arealist = $this->_getAreaModule()->getAreaList(0);
        foreach($arealist as $area) {
            printf('<a href="%s" class="mr10" target="_parent">%s</a>',
                   admin_url('config/areadata', array('pid'=>$area['areaid'])),
                   $area['name']);
        }
    }

    public function indexAction() {
        $pid = intval(get_request('pid', 0));
        if($pid >0 && !$curr = $this->_getAreaModule()->getArea($pid)) {
            $this->renderMessage('区域信息不存在，请重试', admin_url('config/areadata'));
        }

        $fathers = $this->_getAreaModule()->getAncestryIds($pid);
        $route[] = array('areaid'=>0, 'name' => '全部');
        foreach($fathers as $tmpid){
            $temp = $this->_getAreaModule()->getArea($tmpid);
            $route[] = array('areaid'=>$temp['areaid'], 'name' => $temp['name']);
        }

        $list = $this->_getAreaModule()->getAreaList($pid);

        $this->setVar('list',  $list);
        $this->setVar('route', $route);
        $this->setVar('pid',   $pid);
        $this->setVar('level', count($fathers));
        $this->render('admin.config.areadata');
    }

    public function editAction() {
        $areaid = intval(get_request('areaid', 0));
        $data   = $this->_getAreaManager()->get($areaid);

        if(!$data){
            $this->renderMessage('区域信息不存在，请重试', admin_url('config/areadata'));
        }

        if(get_request('submitform')) {
            $pinyin   = get_request('pinyin');
            $letter   = $pinyin{0};
            $joinname = $this->_getAreaManager()->getAncestry($data['pid']);
            $level    = serialize($joinname);

            $this->_getAreaManager()
                 ->set('name',     get_request('name'))
                 ->set('joinname', serialize($joinname))
                 ->set('level',    count($joinname))
                 ->set('pinyin',   strtolower($pinyin))
                 ->set('letter',   strtoupper($letter))
                 ->set('ordnum',   get_request('ordnum', 0))
                 ->update($areaid);

            $this->renderMessage('数据更新成功', null, 'callback.update('.$areaid.')');

        }else{
            $this->setVar('areaid', $areaid);
            $this->setVar('data', $data);
            $this->render('admin.config.areadata_edit');
        }
    }

    public function addAction() {
        $pid   = intval(get_request('pid', 0));
        $data  = $this->_getAreaManager()->get($pid);

        if(!$data){
            $this->renderMessage('区域信息不存在，请重试', admin_url('config/areadata'));
        }

        if(get_request('submitform')) {
            $pinyin   = get_request('pinyin');
            $letter   = $pinyin{0};
            $joinname = $this->_getAreaManager()->getAncestry($pid);
            $level    = serialize($joinname);

            $areaid = $this->_getAreaManager()
                 ->set('pid',      $pid)
                 ->set('name',     get_request('name'))
                 ->set('joinname', serialize($joinname))
                 ->set('level',    count($joinname))
                 ->set('pinyin',   strtolower($pinyin))
                 ->set('letter',   strtoupper($letter))
                 ->set('ordnum',   get_request('ordnum', 0))
                 ->insert();

            $this->renderMessage('数据更新成功', null, 'callback.insert('.$areaid.')');

        }else{
            $this->setVar('pid', $pid);
            $this->render('admin.config.areadata_edit');
        }
    }

    public function delAjaxAction() {
        $areaid = intval(get_request('areaid', 0));
        $subcnt = $this->_getAreaManager()->hasChildren($areaid);
        if($subcnt > 0) {
            $result = array('success'=>false, 'message' => '存在下级区域，无法删除');
        }else{
            $this->_getAreaManager()->delete($areaid);
            $result = array('success'=>true);
        }
        $this->renderJSON($result);
    }

    private function _getAreaModule() {
        return Base::getObject('service.area.AreaModule')->fetchDb();
    }

    private function _getAreaManager() {
        return Base::getObject('service.area.AreaManager');
    }

}


